﻿using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Write.API.Command.UserCommand;
using MediatR;

namespace B.S.BaseData.Write.API.CommandHandler.UserHandler
{
    public class DelUserHandler : IRequestHandler<DelUserCommand, APIResult<object>>
    {
        private readonly IBaseRepository<User> userRep;

        public DelUserHandler(IBaseRepository<User> userRep)
        {
            this.userRep = userRep;
        }
        public async Task<APIResult<object>> Handle(DelUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            
            var a=userRep.GetAll().FirstOrDefault(x=>x.UserId==request.UserId);
            a.IsDeleted= true;

            if (a!=null)
            {
                var b = userRep.UpdateAsync(a).Result;
                if (b>0)
                {
                    result.Code = APIEnum.Success;
                    result.Msg = "删除成功";
                    result.Data = b;

                }
                else
                {
                    result.Code = APIEnum.Failed;
                    result.Msg = "删除失败";
                    result.Data = b;

                }
                
            }
            return await Task.FromResult(result);
        }
    }
}
